
Partial Class SpecialFieldsListItems
    Inherits System.Web.UI.Page

    Protected Sub SaveButton_Click(ByVal sender As Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles SaveButton.Click

        Dim fieldValue As TextBox = CType(SpecialFieldListFormView.FindControl("FieldValueTextBox"), TextBox)
        If fieldValue.Text.Equals("") Then
            Me.ErrorSummary.SetSingleError("Please enter value in the New list item box before saving.")
            Me.ErrorSummary.Visible = True
        Else
            For i As Integer = 0 To SpecialFieldListScrollingGridView.Rows.Count - 1
                If SpecialFieldListScrollingGridView.Rows.Item(0).RowType = DataControlRowType.DataRow Then
                    Dim label As Label
                    label = SpecialFieldListScrollingGridView.Rows(i).FindControl("EditFieldNameLabel")

                    If Not label Is Nothing Then
                        If fieldValue.Text.ToUpper.Equals(label.Text.ToUpper) Then
                            Me.ErrorSummary.SetSingleError("Value already exists in the database. Please enter a different value.")
                            Me.ErrorSummary.Visible = True
                            Exit Sub
                        End If
                    End If
                End If
            Next

            'Past duplicate test.  now add the new item.
            Dim newListItem As Cdm.DataCore.Admin.AdminSpecialFieldListItems = New Cdm.DataCore.Admin.AdminSpecialFieldListItems()
            newListItem.SpecialFieldId = CInt(Request("fieldid"))
            newListItem.FieldValue = fieldValue.Text
            newListItem.Persist()
            Me.SpecialFieldListObjectDataSource.Select()
            Me.SpecialFieldListScrollingGridView.DataBind()
            fieldValue.Text = ""
            SetStatus("New List Item Added")
        End If
    End Sub

    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.SpecialFieldListScrollingGridView.DivClientId, CInt(Me.SpecialFieldListScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.SpecialFieldListScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(28)
    End Sub

    Private Sub ClearStatus()
        SetStatus("")
    End Sub
    Private Sub SetStatus(ByVal message As String)
        Me.SpecialFieldListScrollingGridView.StatusText = message
    End Sub

    Protected Sub CloseButton_Click(ByVal sender As Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles CloseButton.Click

        Response.Redirect("SpecialFields.aspx")

    End Sub



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ClearStatus()
        SetupJavaScript()
        Me.ErrorSummary.Visible = False
        If Not Page.IsPostBack Then
            Me.SpecialFieldListScrollingGridView.Sort("FieldValue", SortDirection.Ascending)
        End If

    End Sub

    Protected Sub SpecialFieldListScrollingGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles SpecialFieldListScrollingGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(SpecialFieldListScrollingGridView)
    End Sub

    Protected Sub SpecialFieldListScrollingGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles SpecialFieldListScrollingGridView.RowCancelingEdit

        If ErrorSummary.Visible = True Then
            ErrorSummary.Visible = False
            SpecialFieldListScrollingGridView.Visible = True
        End If
        Me.SaveButton.Enabled = True
        Me.CloseButton.Enabled = True
        SetStatus("Edit cancelled.")
    End Sub

    Protected Sub SpecialFieldListScrollingGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles SpecialFieldListScrollingGridView.RowEditing

        Me.SaveButton.Enabled = False
        Me.CloseButton.Enabled = False
    End Sub

    Protected Sub SpecialFieldListScrollingGridView_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles SpecialFieldListScrollingGridView.RowUpdated
        Me.SaveButton.Enabled = True
        Me.CloseButton.Enabled = True
        SetStatus("Special List item updated.")
    End Sub

    Protected Sub SpecialFieldListScrollingGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles SpecialFieldListScrollingGridView.RowUpdating
        Dim strNV As String
        Dim sValue As String
        Dim strOV As String

        If e.NewValues.Item(2) IsNot Nothing Then
            strNV = e.NewValues.Item(2).ToString.ToUpper
            If Not String.IsNullOrEmpty(e.OldValues.Item(2)) Then
                strOV = e.OldValues.Item(2).ToString.ToUpper
            Else
                strOV = ""
            End If
            If strOV = strNV Then
                e.Cancel = True

                If Me.ErrorSummary.Visible = True Then
                    Me.ErrorSummary.Visible = False
                End If
            Else
                For i As Integer = 0 To SpecialFieldListScrollingGridView.Rows.Count - 1
                    If SpecialFieldListScrollingGridView.Rows.Item(0).RowType = DataControlRowType.DataRow Then
                        Dim label As Label
                        label = SpecialFieldListScrollingGridView.Rows(i).FindControl("EditFieldNameLabel")

                        If Not label Is Nothing Then
                            If strNV = label.Text.ToUpper Then
                                sValue = "Value already exists in the database. Please enter a different value."
                                Me.ErrorSummary.SetSingleError(sValue)
                                e.Cancel = True
                                Me.ErrorSummary.Visible = True
                                Exit Sub
                            End If
                        End If
                    End If
                Next
                Me.ErrorSummary.Visible = False
                e.NewValues.Item(2) = Server.HtmlEncode(e.NewValues.Item(2).ToString())
            End If
        Else
            sValue = "You must enter a value in the field before updating."
            Me.ErrorSummary.SetSingleError(sValue)
            Me.ErrorSummary.Visible = True
            e.Cancel = True
        End If
    End Sub
End Class
